import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {RobotListService} from './robotList.service';
import {BaseComponent} from '../common/base.component';
import {AddRobotPage, AddRobot, AddMapId} from './robotList';

declare var $: any;

@Component({
  selector: 'app-role-list',
  templateUrl: './robotList.component.html'
})
export class RobotListComponent extends BaseComponent implements OnInit {
  audited: boolean;
  editorTitle: String = '';
  isAddEditor: boolean;
  curPage: Number = 1;
  addUserPage: AddRobotPage;
  addRobot: AddRobot[];
  addMapId: AddMapId[];
  curaddRobot: AddRobot;
  rId: String = '';
  mId: String = '';

  constructor(private robotListService: RobotListService, private router: Router) {
    super();
  }

  ngOnInit() {
    this.curaddRobot = {
      id: '',
      companyName: '',
      robotId: ''
    };
    this.searchUser();
  }

// 绑定机器人
  onEditRobot(value: AddRobot) {
    this.isAddEditor = false;
    this.editorTitle = '绑定地图';
    this.curaddRobot = value;
    this.rId = this.curaddRobot.id;
    $('#data_editor').modal('show'); // 显示编辑对话框
    this.getMapIdInfo();
  }

// 查询地图Id
  getMapIdInfo() {
    this.robotListService.addMapId()
      .then(res => {
          if (res[0]) {
            this.addMapId = res[1];
          }
        }
      );
  }

// 新增/修改
  save() {
    // 修改数据
    this.robotListService.saveMapIdinfo(this.curaddRobot, this.rId, this.mId)
      .then(result => {
        if (result) {
          this.alert.info('保存成功');
          this.queryList();
        } else {
          this.alert.error('保存失败');
        }
      });


    $('#data_editor').modal('hide');
  }

  // 查询机器人
  searchUser() {
    this.curPage = 1;
    this.queryList();
  }

  onPage(event) {
    this.curPage = event;
    this.queryList();
  }

  private queryList() {
    this.robotListService.getRobotList(this.curPage)
      .then(page => {
          this.addUserPage = page;
          this.addRobot = page.addRobot.datas;
          for (const index of this.addRobot) {
            if (index.companyName === '') {
              this.audited = true;
            } else {
              this.audited = false;
            }
          }
        }
      );
  }

  selMid(id) {
    this.mId = id;
  }
}
